package SeverMake;

import Tools.GetScanner;
import Tools.User;
import Tools.UserMessage;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class SeverMaking {
    private List<User> list = new ArrayList<>();
    private ObjectInputStream ois = null;
    private ObjectOutputStream oos = null;
    private ServerSocket ss;
    private Socket socket;
    private  File file = new File("d:/student.txt");
    public void severStart() throws IOException, ClassNotFoundException {
        ss = new ServerSocket(8888);
        System.out.println("等待客户端连接！");
        socket = ss.accept();
        System.out.println("客户端连接成功！");
        read();
        severReceive();
    }

    public void read() throws IOException, ClassNotFoundException {
        if (file.exists()){
            ObjectInputStream ois2 = new ObjectInputStream(new FileInputStream(file));
            list = (List<User>)ois2.readObject();
            System.out.println("已有的学生：" + list);
        }else {
            list = new ArrayList<>();
        }
    }

    public void severClose() throws IOException {
        if (null != socket){
            socket.close();
        }if (null != ss){
            ss.close();
        }if (null != oos){
            oos.close();
        }if (null != ois){
            ois.close();
        }GetScanner.closeSc();
    }

    public void severReceive() throws IOException, ClassNotFoundException {
        oos = new ObjectOutputStream(socket.getOutputStream());
        ois = new ObjectInputStream(socket.getInputStream());
        while (true){
            UserMessage um = (UserMessage) ois.readObject();
            System.out.println("接收到客户端发来的内容是：" + um.getType());
            switch (um.getType()){
                case "managerCheck":
                    if (managerCheck(um.getUser())){
                        um.setType("success");
                    }else {
                        um.setType("fail");
                    }
                    oos.writeObject(um);
                    System.out.println("校验结果发送成功！");
                    break;
                case "studentCheck":
                    if (studentCheck(um.getUser())){
                        um.setType("success");
                    }else um.setType("fail");
                    oos.writeObject(um);
                    System.out.println("校验结果发送成功！");
                    break;
                case "addStudent":
                    addList(um);
                    break;
                case "changeStudent":
                    change(um);
                    break;
                case "exit":
                    return;
            }
        }
    }

    private void change(UserMessage um) throws IOException {
        for (User user : list){
            if (user.getName().equalsIgnoreCase(um.getUser().getName()) &&
                user.getPassword().equalsIgnoreCase(um.getUser().getPassword())
            ){
                //要修改的学员必须存在才能修改！
                System.out.println("该学员存在集合中，可以修改信息！");
                System.out.println("要修改的姓名！");
                String changeName = GetScanner.getSc().next();
                user.setName(changeName);
                System.out.println("要修改的密码！");
                String changePassword = GetScanner.getSc().next();
                user.setPassword(changePassword);
                um.setType("success");
                oos.writeObject(um);
                System.out.println("修改后的信息发送成功！");
                ObjectOutputStream oos2 = new ObjectOutputStream(new FileOutputStream(file));
                //将修改后的学生信息写入到文件中！
                list.add(um.getUser());
                oos2.writeObject(list);
                oos2.close();
                GetScanner.closeSc();
                return;
            }
                System.out.println("该学员不存在无法修改相关信息！");
                um.setType("fail");
                oos.writeObject(um);
        }
    }

    public void addList(UserMessage um) throws IOException {
        //判断添加的学员，服务器是否存在
        for (User user : list){
            if (user.getName().equalsIgnoreCase(um.getUser().getName())){
                um.setType("fail");
                System.out.println("该学生已存在集合中！");
                oos.writeObject(um);
                return;
            }
        }
        //添加成功，通知客户端成功表示：success
        um.setType("success");
        oos.writeObject(um);
        //把学员添加到集合中
        list.add(um.getUser());
        //有新的学生 添加到服务器，接下来把新的信息写入到服务器的文件中。
        ObjectOutputStream oos2 = new ObjectOutputStream(new FileOutputStream(file));
        oos2.writeObject(list);
        oos2.close();
    }

    public boolean studentCheck(User user){
        for (User userStudent : list ){
            if (userStudent.getName().equalsIgnoreCase(user.getName()) &&
                    userStudent.getPassword().equalsIgnoreCase(user.getPassword())){
                return true;
            }
        }return false;

    }

    public boolean managerCheck(User user){
        if ("admin".equalsIgnoreCase(user.getName()) &&
          "123456".equalsIgnoreCase(user.getPassword())
        ){
            return true;
        }
            return false;
    }

}
